﻿using Manon.Autofac;
using Manon.Core.Commons;
using Manon.Gateway.ApplicationCore.Dtos;
using Manon.Gateway.ApplicationCore.Dtos.Routes;
using Manon.Gateway.ApplicationCore.Interfaces;
using Manon.Repository.Collections;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Manon.Gateway.Admin.Controllers
{
    public class RoutesController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Project()
        {
            return View();
        }

        public IActionResult ProjectEdit()
        {
            return View();
        }

        public IActionResult Edit()
        {
            return View();
        }

        /// <summary>
        /// 获取项目分页列表
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IPagedList<GetProjectPageListOutput>> GetProjectPageList([FromBody]GetProjectPageListInput Input)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.GetProjectPageList(Input);
            }
            catch (Exception ex)
            {
                logger.LogError($"获取项目分页列表异常,参数Input={JsonConvert.SerializeObject(Input)} ", ex);
                throw;
            }
        }

        /// <summary>
        /// 获取项目信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<GetProjectOutput> GetProject(int Id)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.GetProject(Id);
            }
            catch (Exception ex)
            {
                logger.LogError($"获取项目信息异常,参数Id={Id} ", ex);
                throw;
            }
        }

        /// <summary>
        /// 设置项目启用/禁用
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Enabled"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetProjectEnabled(int Id, bool Enabled)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.SetProjectEnabled(Id, Enabled);
            }
            catch (Exception ex)
            {
                logger.LogError($"设置项目启用/禁用 异常,参数Id={Id}Enabled={Enabled}", ex);
                throw;
            }
        }


        /// <summary>
        /// 保存项目
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SaveProject([FromBody] SaveProjectInput Input)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.SaveProject(Input); ;
            }
            catch (Exception ex)
            {
                logger.LogError($"保存项目异常,参数input={JsonConvert.SerializeObject(Input)}", ex);
                throw;
            }
        }





        /// <summary>
        /// 保存路由设置
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SaveRoute([FromBody] SaveRouteInput Input)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.SaveRoute(Input); ;
            }
            catch (Exception ex)
            {
                logger.LogError($"保存路由设置异常,参数input={JsonConvert.SerializeObject(Input)}", ex);
                throw;
            }
        }

        /// <summary>
        /// 获取路由信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<GetRoutesOutput> GetRoutes(int Id)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.GetRoutes(Id); ;
            }
            catch (Exception ex)
            {
                logger.LogError($"获取路由信息异常,参数Id={Id}", ex);
                throw;
            }
        }


        /// <summary>
        /// 获取项目路由分页列表
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IPagedList<GetProjectRoutesPageListOutput>> GetProjectRoutesPageList([FromBody]GetProjectRoutesPageListInput Input)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IRouteService>();
                return await routeService.GetProjectRoutesPageList(Input); ;
            }
            catch (Exception ex)
            {
                logger.LogError($"获取项目路由分页列表异常,参数Input={JsonConvert.SerializeObject(Input)}", ex);
                throw;
            }
        }
    }
}
